package com.example.a2302fx.fg.minefg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.a2302fx.R
import com.example.a2302fx.adapter.OrderAdapter
import com.example.a2302fx.databinding.FragmentPayingBinding
import com.example.base.mvvm.BaseFragment


class PayingFragment : BaseFragment<FragmentPayingBinding,OrderViewModel>() {
    lateinit var orderAdapter: OrderAdapter
    override fun initData() {
        orderAdapter = OrderAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)

        viewModel.order("0",10,1)
        //点击更多，取消订单
        orderAdapter.addChildClickViewIds(R.id.tv_more,R.id.tv_addCar)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.tv_more->{
                    //调用接口
                    viewModel.cancel(orderAdapter.data[position].id)
                    //页面移除
                    orderAdapter.data.removeAt(position)
                    orderAdapter.notifyDataSetChanged()
                }
                R.id.tv_addCar->{
                    viewModel.add(orderAdapter.data[position].orderInfoList[0].cartNum,orderAdapter.data[position].orderInfoList[0].productId.toString(),orderAdapter.data[position].orderInfoList[0].productId)
                }
            }
        }
    }

    override fun initView() {
       viewModel.orderSuccess.observe(this){
           if(it.code==200){
               orderAdapter.data.clear()
               orderAdapter.data.addAll(it.data.list)
               orderAdapter.notifyDataSetChanged()
           }
       }
        viewModel.cancelSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_paying

}